/**
 * 
 */
package net.ddns.heroli.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.gzheroli.bfw.pojo.base.TSBaseUser;
import cn.com.gzheroli.bfw.pojo.base.TSUser;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.model.json.AjaxJson;
import cn.com.gzheroli.core.common.model.json.TreeGrid;
import cn.com.gzheroli.core.common.service.CommonService;
import cn.com.gzheroli.core.common.service.HeroService;
import cn.com.gzheroli.core.common.service.SystemService;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.util.NameHandler;
import cn.com.gzheroli.core.util.RequestUtil;
import cn.com.gzheroli.tag.core.DataGrid;
import cn.com.gzheroli.util.GenSqlUtil;
import net.ddns.heroli.pojo.TBSProType;
import net.ddns.heroli.pojo.TBShop;

/**  
* 类名: StoreController  
* 描述: 商铺管理 
* 作者: 李长荣 lichang_20011@163.com  
* 日期: 2017年12月19日  下午3:53:32
*@version V1.0  
*    
*/
@Controller
@RequestMapping("/api/shopController")
public class ShopController {
	private static final Logger logger = LoggerFactory.getLogger(ShopController.class);
	@Autowired
	private CommonService commonService;
	@Autowired
	private HeroService heroService;
	@Autowired
    private SpringRedisCache cache;
	@Autowired
	private SystemService systemService;
	@RequestMapping(value = "del")
	@ResponseBody
	public AjaxJson del(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		TBShop shop=commonService.get(TBShop.class, id);
		shop.setUpdateBy(user.getId());
		shop.setUpdateTime(new Date());
		shop.setUpdateByName(user.getRealName());
		shop.setStatus("0");
		commonService.update(shop);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}
	@RequestMapping(value = "get")
	@ResponseBody
	public AjaxJson get(String id, HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("id不存在!");
			return j;
		}
		Map para=new HashMap();
		para.put("id", id);
		//TBShop shop=commonService.get(TBShop.class, id);
		String sql="select s.*,bu.REAL_NAME,bu.USER_NAME,u.* from T_B_SHOP s,T_S_BASE_USER bu,T_S_USER u ";
		sql=sql+" where s.ID=bu.ID and bu.ID=u.ID and bu.STATUS='1' and bu.ID=#id#";
		Map ret=heroService.findOneObjForJdbc(sql, para, Map.class);
		j.setObj(ret);
		j.setSuccess(true);
		j.setMsg("获取成功!");
		return j;
	}
	@RequestMapping(value = "save")
	@ResponseBody
	public  AjaxJson save(TBShop shop, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		TSUser user=client.getUser();
		if(shop.getId()==null){
			shop.setUpdateBy(user.getId());
			shop.setUpdateTime(new Date());
			shop.setUpdateByName(user.getRealName());
			shop.setStatus("1");
			commonService.save(shop);
		}else{
			shop.setUpdateBy(user.getId());
			shop.setUpdateTime(new Date());
			shop.setUpdateByName(user.getRealName());
			commonService.update(shop);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "update")
	@ResponseBody
	public  AjaxJson update( HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			j.setSuccess(false);
			j.setMsg("尚未登录,请登录!");
			return j;
		}
		String id=request.getParameter("id")==null?"":request.getParameter("id");
		TSUser user=client.getUser();
		if(id==null||id.trim().equals("")){
			j.setSuccess(false);
			j.setMsg("保存失败!,编号不能为空");
			return j;
		}else{
			Map reqMap=RequestUtil.convertParameterToMap(request);
			reqMap.put("updateBy", user.getId());
			reqMap.put("updateTime", new Date());
			reqMap.put("updateByName", user.getRealName());
			heroService.updateMap("T_B_SHOP", reqMap, null, TBShop.class);
		}
		j.setSuccess(true);
		j.setMsg("保存成功!");
		return j;
	}
	@RequestMapping(value = "list")
	@ResponseBody
	public  AjaxJson  list(HttpServletRequest request, HttpServletResponse response, TreeGrid treegrid) {
		AjaxJson j = new AjaxJson();
		String page=request.getParameter("page")==null?"1":request.getParameter("page");
		String rows=request.getParameter("rows")==null?"1000":request.getParameter("rows");
		//Client client=cache.getCache(request.getSession().getId(), Client.class);
		String token = request.getHeader("token")==null?request.getParameter("token"):request.getHeader("token");
		Client client=cache.getCache(token, Client.class);
		if(client==null||client.getUser()==null){
			//return null;
		}else{
			TSUser user=client.getUser();
		}
		Map reqMap=RequestUtil.convertParameterToMap(request);
		List<Map<String,Object>> treeGrids = new ArrayList<Map<String,Object>>();
		Map conMap=new HashMap();
		conMap.put("status", "=");
		if(!reqMap.containsKey("status"))
			reqMap.put("status", "1");
		if(reqMap.containsKey("proId"))
		conMap.put("proId", "=");
		if(reqMap.containsKey("name"))
		conMap.put("name", "like");
		if(reqMap.containsKey("code"))
			conMap.put("code", "like");
		Class clazz=TBShop.class;
		String tableName = new NameHandler().getTableName(clazz.getSimpleName());
		Map<String,Object> tableColumns=systemService.getHeroTableColumns(tableName,clazz);
		String select="s.*,bu.REAL_NAME,bu.USER_NAME,u.*";
		String sql="  from T_B_SHOP s,T_S_BASE_USER bu,T_S_USER u ";
		sql=sql+" where s.ID=bu.ID and bu.ID=u.ID and bu.STATUS='1' ";
		String whereSql="";
		whereSql=GenSqlUtil.genWhereSql(tableName, conMap, tableColumns, "s", reqMap);
		Map<String,Object> userColumns=systemService.getHeroTableColumns("T_S_USER",TSUser.class);
		whereSql=whereSql+GenSqlUtil.genWhereSql("T_S_USER", conMap, userColumns, "u", reqMap);
		List whereSqlList=new ArrayList();
		whereSqlList.add(" 1=1 "+whereSql);
		reqMap.put("whereSql", " 1=1 "+whereSql);
		PageQuery pg=heroService.findForSqlIdPage("shop.selectShop", reqMap,Long.parseLong(page),Long.parseLong(rows));
		//DataGrid grid=heroService.getDataGridReturn(sql, reqMap,select,Map.class, Integer.parseInt(page), Integer.parseInt(rows));
		//String sql=GenSqlUtil.genSqlColumnsByClass(clazz, conMap,"list",tableColumns,"am",reqMap,);
		//List beanList =commonService.findForJdbcPage(sql, Map.class, reqMap, Integer.parseInt(page), Integer.parseInt(rows));
		j.setObj(pg.getList());
		//String countSql=GenSqlUtil.genSqlByClass(clazz, conMap,"count",tableColumns,"am",reqMap);
		//Long total=commonService.getCountForJdbc(countSql, reqMap);
		//j.setTotal(Long.parseLong(grid.getTotal()+""));
		j.setTotal(pg.getTotalRow());
		return j;
	}
}
